﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace UCSD_BioLit
{
    partial class AboutBox : Form
    {
        public AboutBox()
        {
            InitializeComponent();

            // Set the control to autosize based on the text content.
            linkLabel1.AutoSize = true;
            // Set the text to display in the label.
            linkLabel1.Text = "http://www.codeplex.com/UCSDBioLit";

            Application.EnableVisualStyles(); 
            this.Load += new EventHandler(AboutBox_Load);
        }

        void AboutBox_Load(object sender, EventArgs e)
        {
            try
            {
                Microsoft.Win32.RegistryKey localMachine = Microsoft.Win32.Registry.LocalMachine;
                Microsoft.Win32.RegistryKey productDirectory = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
                if (productDirectory != null && productDirectory.SubKeyCount > 0)
                {
                    string[] products = productDirectory.GetSubKeyNames();
                    foreach (string product in products)
                    {
                        Microsoft.Win32.RegistryKey productDetails = productDirectory.OpenSubKey(product);
                        if (ValueNameExists(productDetails.GetValueNames(), "DisplayName") &&
                        ValueNameExists(productDetails.GetValueNames(), "DisplayVersion"))
                        {
                            if (string.Compare(productDetails.GetValue("DisplayName").ToString(), "Word Add in For Ontology Recognition", true, System.Globalization.CultureInfo.CurrentCulture) == 0)
                            {
                                this.lblVersionNumber.Text = productDetails.GetValue("DisplayVersion").ToString();
                                break;
                            }
                        }
                        productDetails.Close();
                    }
                    productDirectory.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }

        }

        private bool ValueNameExists(string[] valueNames, string valueName)
        {
            foreach (string s in valueNames)
            {
                if (s.ToLower() == valueName.ToLower()) return true;
            }

            return false;
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.codeplex.com/UCSDBioLit");
        }
    }
}
